
from selenium.webdriver.common.by import By

from page.net_project_page import NetProjectPage
from common.page import IOS, XPATH
from selenium.common.exceptions import NoSuchElementException
import random as r
import time as t


class XbAchievementPage(NetProjectPage):
    # 通用按钮
    release_btn = (IOS, "type == 'XCUIElementTypeButton' AND label == '提交'")
    confirm_btn = (IOS, "type == 'XCUIElementTypeButton' AND label == '确认'")
    confirm_btn_2 = (IOS, "type == 'XCUIElementTypeButton' AND label == '确定'")
    finish_btn = (IOS, "type == 'XCUIElementTypeButton' AND label == '完成'")

    # 分类选择相关
    list_choose = (IOS, "type == 'XCUIElementTypeStaticText' AND label == '请选择分类：'")
    list_all = (IOS, "type == 'XCUIElementTypeStaticText' AND label == '全部'")  # 用第二个
    list_mine = (IOS, "type == 'XCUIElementTypeStaticText' AND label == '我的成果'")
    list_recommended = (IOS, "type == 'XCUIElementTypeStaticText' AND label == '已推荐'")
    list_not_recommended = (IOS, "type == 'XCUIElementTypeStaticText' AND label == '未推荐'")

    # 提交页面
    # title = (By.XPATH, "//XCUIElementTypeStaticText[contains(@name,'请输入成果标题')]")
    title = (IOS, "type == 'XCUIElementTypeTextView' AND value == '请输入成果标题'")
    classify_choose = (IOS, "type == 'XCUIElementTypeStaticText' AND name == '选择分类'")
    discuss_input = (XPATH, "XCUIElementTypeStaticText[contains(@name,'请输入成果描述')]/../XCUIElementTypeTextView")
    attachment_btn = (IOS, "type == 'XCUIElementTypeButton' AND name == '附件'")
    camera_btn = (IOS, "type == 'XCUIElementTypeButton' AND label == 'xiangji'")
    photo_btn = (IOS, "type == 'XCUIElementTypeImage' AND name == 'hVideo_take'")
    video_name_input = (XPATH, "//XCUIElementTypeStaticText[@name='命名']/../XCUIElementTypeTextField")

    # 查看成果
    consult_btn = (IOS, "type == 'XCUIElementTypeButton' AND name == '查阅'")
    marking_btn = (IOS, "type == 'XCUIElementTypeButton' AND name == '批阅'")
    recommend_radio = (IOS, "type == 'XCUIElementTypeButton' AND name == '推荐'")
    comment_input = (XPATH, "//XCUIElementTypeStaticText[contains(@name,'写评')]/../XCUIElementTypeTextView")
    static_for_click = (IOS, "type == 'XCUIElementTypeStaticText' AND name == '查阅'")  # 点击以收回键盘
    static_for_click_2 = (IOS, "type == 'XCUIElementTypeStaticText' AND name == '批阅'")  # 点击以收回键盘
    excellent_btn = (IOS, "type == 'XCUIElementTypeButton' AND label == '优秀'")
    great_btn = (IOS, "type == 'XCUIElementTypeButton' AND label == '良好'")
    good_btn = (IOS, "type == 'XCUIElementTypeButton' AND label == '合格'")
    bad_btn = (IOS, "type == 'XCUIElementTypeButton' AND label == '不合格'")

    def enter_achievement(self, title="自动化成果"):
        for i in range(5):
            try:
                self.find_element(IOS, "type == 'XCUIElementTypeStaticText' AND label CONTAINS '%s'" % title).click()
            except NoSuchElementException:
                t.sleep(0.5)
                self.swipe_dr("up")
                continue
            else:
                break

    def release_achievement(self):
        # self.find_element(*self.list_choose).click()
        # self.find_element(*self.list_mine).click()
        # t.sleep(3)

        self.find_element(*self.release_btn).click()
        self.swipe(30,100,30,100)
        # title_input = (IOS, "type == 'XCUIElementTypeStaticText' AND name == '请输入成果标题'")
        # self.find_element(*self.title).send_keys('自动化成果长度小于40的测试标题')
        # self.find_element(By.XPATH,"//XCUIElementTypeTextView[1]/XCUIElementTypeStaticText[1]").send_keys("成果标题")
        self.find_static_text(string="请输入成果标题",contains_flag=1).send_keys("自动化成果长度小于40的测试标题")
        self.find_element(*self.classify_choose).click()
        t.sleep(2)
        self.find_element(*self.confirm_btn).click()
        self.swipe(30,310,30,310)
        self.find_static_text(string="请输入成果描述",contains_flag=1).send_keys("这是成果的描述。成果秒速内容好像不是太丰富总数在这哈哈哈级好的你奥德的不是加继续加油啊的诶欧文画册我欧文hi偶尔玩。。")
        self.hide_keyboard()
        self.find_element(*self.attachment_btn).click()
        self.find_element(*self.camera_btn).click()
        t.sleep(3)
        photo = self.find_element(*self.photo_btn)
        self.touch_and_hold(photo, 6.5)
        t.sleep(3)
        self.find_element(*self.finish_btn).click()
        t.sleep(2)
        self.find_element(*self.video_name_input).send_keys("视频的名字")
        self.find_element(*self.confirm_btn_2).click()

        for i in range(5):  # 等待弹窗消失
            try:
                t.sleep(1)
                self.find_static_text(string="视频上传中，请勿离开页面！", times=2)
            except NoSuchElementException:
                break
        # for i in range(20):
        #     try:
        #         self.find_element(*self.confirm_btn).click()
        #         # self.find_element(*self.release_btn, times=2).click()
        #         self.find_element(XPATH,'//*[@name="提交"]',times=4).click()
        #     except NoSuchElementException:
        #         t.sleep(1)
        #         continue
        #     else:
        #         break
        self.find_element(*self.confirm_btn).click()  #确定
        self.find_element(XPATH, '//*[@name="提交"]', times=4).click()
        self.enter_achievement()
        t.sleep(1)
        # self.find_button("评论").click()
        self.find_element(XPATH,'//*[@name="评论"]').click()
        t.sleep(3)
        self.get_frame_center_and_click(self.find_text_field(string="发布评论", attr='value'), flag=0)
        # self.find_element(XPATH,"//XCUIElementTypeTextField[contains(@value,'发布评论')]").click()
        self.find_element(XPATH,"//XCUIElementTypeStaticText[contains(@name,'请输入内容')]/../XCUIElementTypeTextView").send_keys("成果评论内容显示展示好的吧是多少机动式为耦合我超级搜无文本框无斯巴鲁ueiw并从啊报错不二级不还得靠好的技术这样干加油还是把护士回吧爸爸的马克")
        self.find_button("发布").click()
        self.find_button(string="返回",contains_flag=1).click()
        self.find_button(string="返回",contains_flag=1).click()
        self.find_button(string="返回", contains_flag=1).click()
        self.find_button(string="common back light icon",contains_flag=1).click()

    def consult_achievement(self):
        self.enter_achievement()
        t.sleep(1)
        self.find_element(*self.consult_btn).click()
        t.sleep(1)
        self.find_element(*self.recommend_radio).click()
        self.find_element(*self.comment_input).send_keys("这是评语。。。。。")
        self.hide_keyboard()
        # self.find_element(*self.static_for_click).click()
        self.find_element(*self.confirm_btn).click()
        self.find_element(*self.finish_btn).click()
        self.find_button(string="返回", contains_flag=1).click()


    def marking_achievement(self):
        self.enter_achievement()
        t.sleep(1)
        self.find_element(*self.marking_btn).click()
        t.sleep(1)
        self.find_element(*self.recommend_radio).click()
        self.find_element(*self.great_btn).click()
        self.find_element(*self.comment_input).send_keys("这是老师对学生发布的成果进行的评语。友好有坏大家继续加油哦继续加油贾诩UI额外会厚度为IQ回到屋我北大壶我IQhi此仇不报非君子。。。。")
        self.find_element(*self.static_for_click_2).click()
        self.find_element(*self.confirm_btn).click()
        self.find_element(*self.finish_btn).click()
        self.find_button(string="返回", contains_flag=1).click()
        self.swipe_dr('down')
        t.sleep(2)
        self.find_button(string="返回", contains_flag=1).click()
        self.find_button(string="common back light icon", contains_flag=1).click()


if __name__ == "__main__":
    test = XbAchievementPage()
    test.enter_project()
    test.enter_tools("成果")
    test.consult_achievement()
